package com.ccb.p4.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ccb.p4.common.R;
import com.ccb.p4.entity.Problem;
import com.ccb.p4.entity.vo.ProblemQuery;
import com.ccb.p4.service.ProblemService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * p4问题错误表 前端控制器
 * </p>
 *
 * @author xianghui.zh
 * @since 2022-01-19
 */
@RestController
@RequestMapping("/ccb/problem")
public class ProblemController {

    @Autowired
    private ProblemService problemService;

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有不分页")
    @GetMapping("all")
    public R listAll() {
        List<Problem> problems = problemService.listAll();
        return R.ok().data("all", problems);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询问题")
    @GetMapping("getProblem/{id}")
    public R findById(
            @ApiParam(name = "id", value = "问题ID", required = true)
            @PathVariable String id) {
        Problem item = problemService.findById(id);
        return R.ok().data("item", item);
    }


    /**
     * 分页组合查询
     *
     * @param page
     * @param limit
     * @param problemQuery
     * @return
     */
    @ApiOperation(value = "多条件分页组合查询问题")
    @PostMapping("list/{page}/{limit}")
    public R queryProblem(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "problemQuery", value = "查询对象Vo", required = false)
            @RequestBody ProblemQuery problemQuery) {
        Page<Problem> pagePram = new Page<>(page, limit);
        problemService.pageQuery(pagePram, problemQuery);
        List<Problem> records = pagePram.getRecords();
        long total = pagePram.getTotal();
        return R.ok().data("total", total).data("items", records);
    }

    /**
     * 添加问题
     *giut
     * @param problem
     * @return
     */
    @ApiOperation(value = "添加问题")
    @PostMapping("addProblem")
    public R addProblem(
            @ApiParam(name = "problem", value = "问题对象", required = true)
            @RequestBody Problem problem) {
        problemService.add(problem);
        return R.ok();
    }

    /**
     * 修改问题
     * @param problem
     * @return
     */
    @ApiOperation(value = "修改问题")
    @PostMapping("updateProblem")
    public R update(@RequestBody Problem problem){
        boolean b = problemService.updateProblem(problem);
        return b == true ? R.ok() : R.error();
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除问题")
    @DeleteMapping("{id}")
    public R deleteById(
            @ApiParam(name = "id", value = "问题ID", required = true)
            @PathVariable Integer id) {
        boolean b = problemService.deleteById(id);
        return b == true ? R.ok() : R.error().message("删除失败");
    }

}

